package com.yanti.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yanti.domain.entity.UserWrongQuestions;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;

import java.util.List;


/**
 * 用户错题本表(UserWrongQuestions)表数据库访问层
 *
 * @author luo
 * @since 2025-04-07 16:30:28
 */
public interface UserWrongQuestionsMapper extends BaseMapper<UserWrongQuestions> {
    @Insert({
            "<script>",
            "INSERT INTO user_wrong_questions (uid, question_id, subject_id, chapter_id, user_answer, difficulty, error_count, last_practiced_at, is_mastered, created_at, updated_at)",
            "VALUES ",
            "<foreach collection='list' item='item' separator=','>",
            "(#{item.uid}, #{item.questionId}, #{item.subjectId}, #{item.chapterId},#{item.userAnswer}, #{item.difficulty}, 1, #{item.lastPracticedAt}, #{item.isMastered}, #{item.createdAt}, #{item.updatedAt})",
            "</foreach>",
            "ON DUPLICATE KEY UPDATE ",
            "user_answer = VALUES(user_answer), ",
            "error_count = error_count + VALUES(error_count), ", // 通过唯一索引触发更新
            "last_practiced_at = VALUES(last_practiced_at), ",
            "updated_at = VALUES(updated_at)",
            "</script>"
    })
    void insertOrUpdateBatch(@Param("list") List<UserWrongQuestions> records);
}

